/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.nViron.Position;

import instar.dg.Debug.Debug;
import instar.dg.Positioning.SiemensWlanPositioning;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 *
 * @author Stefan
 */
public class SiemensWlanPositioningSE extends SiemensWlanPositioning {

        public SiemensWlanPositioningSE(String serverRequestURL) {
            super(serverRequestURL);
        }

        protected byte[] getInputData() {
        HttpURLConnection c = null;
        InputStream is = null;
        int rc;

        try {
            URL _url = new URL(url);
            Debug.out.println(System.currentTimeMillis() + " ms - SiemensWlanPositioning Polling Thread: requestPosition: Connector.open(" + url + ") !");
            c = (HttpURLConnection) _url.openConnection();

            // Getting the response code will open the connection,
            // send the request, and read the HTTP response headers.
            // The headers are stored until requested.
            rc = c.getResponseCode();
            Debug.out.println(System.currentTimeMillis() + " ms - SiemensWlanPositioning Polling Thread: getResponseCode: " + rc + "!");
            if (rc != HttpURLConnection.HTTP_OK) {
                invokeFailure("HTTP response code: " + rc);
                return null;
            }

            is = c.getInputStream();

            // Get the ContentType
            String type = c.getContentType();

            // Get the length and process the data
            int len = (int) c.getContentLength();

            if (len > 0) {
                int actual = 0;
                int bytesread = 0;
                byte[] buffer = new byte[len];
                while ((bytesread != len) && (actual != -1)) {
                    actual = is.read(buffer, bytesread, len - bytesread);
                    bytesread += actual;
                }

                return buffer;
            } else {
                byte[] buffer = new byte[512];
                int idx = 0;
                while ((buffer[idx++] = (byte) is.read()) != -1) {
                }
                return buffer;
            }
        } catch (IOException ex) {
            invokeFailure("WlanPositioning encountered an Error: " + ex.toString());
            return null;
        } catch (ClassCastException e) {
            invokeFailure("WlanPositioning encountered an Error: given URL is not a valid HTTP URL!");
            return null;
        } catch (Throwable t) {
            t.printStackTrace();
            Debug.out.println(System.currentTimeMillis() + " ms - SiemensWlanPositioning Polling Thread throws '" + t.toString() + "'!");
        } finally {
            try {
                if (is != null) {
                    is.close();
                }
                if (c != null) {
                    c.disconnect();
                }
            } catch (IOException io) {
            }
        }
        return null;
    }
}

